package com.logic.landseaserver.ws;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.PropertyCheck;
import com.logic.landseaserver.domain.PropertyCheckEstimation;
import com.logic.landseaserver.service.IPropertyCheckService;
import com.logic.landseaserver.ws.dto.LogsDTO;
import com.logic.landseaserver.ws.dto.PropertyCheckDTO;
import com.logic.landseaserver.ws.dto.PropertyCheckEstimationDTO;
import com.logic.landseaserver.ws.dto.PropertyCheckLogDTO;
import com.logic.landseaserver.ws.request.PropertyCheckDeleteReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by Tony on 2017/4/12.
 */


@Path("/PropertyCheck")
@Api(value = "propertyCheck", description = "", position = 42)
public class PropertyCheckRest {
    @Autowired
    private IPropertyCheckService propertyCheckServiceImpl;

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    //保存上传的物业
    @POST
    @Path("/addPropertyCheck")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "add PropertyCheck.")
    public Response addPropertyCheck(
            @ApiParam(value = "param", required = true) PropertyCheck propertyCheck) {
        CommonResultMap map = new CommonResultMap();
        try {
            int a = propertyCheckServiceImpl.insertPropertyCheck(propertyCheck);
            map.setData(a);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("addPropertyCheck error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("addPropertyCheck error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //物业审查表
    @POST
    @Path("/query/{propertyId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "get room list.")
    public Response queryProjectAppartments(
            @ApiParam(value = "param", required = true) @PathParam("propertyId") Integer propertyId,
            QueryUtil queryUtil) {
        CommonResultMap map = new CommonResultMap();
        List<PropertyCheckDTO> propertyCheckList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            propertyCheckList = propertyCheckServiceImpl.queryByPage(queryUtil, propertyId);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(propertyCheckList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);
        } catch (Exception e) {
            logger.error("queryProjectAppartments error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //审查
    @POST
    @Path("/addCheck")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "add Check.")
    public Response addCheckResult(
            @ApiParam(value = "param", required = true) PropertyCheckEstimation propertyCheckEstimation) {
        CommonResultMap map = new CommonResultMap();
        try {
            int a = propertyCheckServiceImpl.insertCheckResult(propertyCheckEstimation);
            map.setData(a);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("addCheckResult error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("addCheckResult error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //审查历史
    @POST
    @Path("/getChecks/{propertyCheckId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "get all Check.")
    public Response getCheckResults(
            @ApiParam(value = "param", required = true) @PathParam("propertyCheckId") Integer propertyCheckId) {
        CommonResultMap map = new CommonResultMap();
        try {
            List<PropertyCheckEstimationDTO> propertyCheckEstimation = propertyCheckServiceImpl.getCheckResults(propertyCheckId);
            map.setData(propertyCheckEstimation);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("getCheckResults error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("getCheckResults error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //足迹
    @POST
    @Path("/getRecords/{propertyCheckId}/{modules}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "get all Record.")
    public Response getRecords(
            @ApiParam(value = "param", required = true) @PathParam("propertyCheckId") Integer propertyCheckId,
            @ApiParam(value = "param", required = true) @PathParam("modules") Integer modules) {
        CommonResultMap map = new CommonResultMap();
        try {
            List<PropertyCheckLogDTO> propertyCheckActionLogs = propertyCheckServiceImpl.getRecords(propertyCheckId,modules);
            map.setData(propertyCheckActionLogs);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (Exception e) {
            logger.error("getRecords error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //删除
    @DELETE
    @Path("/deletePropertyCheck")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "删除上传记录")
    public Response deletePropertyCheck(@ApiParam(value = "param", required = true) PropertyCheckDeleteReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertyCheckServiceImpl.deletePropertyChecks(req);
            map.setResult(CommonResult.SUCCESS, "delete success");
        } catch (Exception e) {
            logger.error("deletePropertyCheck error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    //日志
    @POST
    @Path("/addLog")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "add log.")
    public Response addLog(
            @ApiParam(value = "param", required = true) LogsDTO log) {
        CommonResultMap map = new CommonResultMap();
        try {
            propertyCheckServiceImpl.insertLog(log);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("addLog error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("addLog error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }


}


